package com.nbcio.iot.controller;

import com.nbcio.common.core.domain.R;

import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.iot.domain.bo.IotDeviceGroupBo;
import com.nbcio.iot.domain.vo.IotDeviceGroupVo;
import com.nbcio.iot.service.IIotDeviceGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备分组Controller
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/device-group")
public class IotDeviceGroupController {

    private final IIotDeviceGroupService groupService;

    /**
     * 查询设备分组列表
     */
    @GetMapping("/list")
    public R<List<IotDeviceGroupVo>> list(IotDeviceGroupBo bo) {
        List<IotDeviceGroupVo> list = groupService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 分页查询设备分组列表
     */
    @GetMapping("/page")
    public TableDataInfo<IotDeviceGroupVo> page(IotDeviceGroupBo bo) {
        List<IotDeviceGroupVo> list = groupService.queryList(bo);
        return TableDataInfo.build(list);
    }

    /**
     * 查询设备分组树形列表
     */
    @GetMapping("/tree")
    public R<List<IotDeviceGroupVo>> treeList() {
        List<IotDeviceGroupVo> list = groupService.queryTreeList();
        return R.ok(list);
    }

    /**
     * 获取设备分组详细信息
     */
    @GetMapping("/{groupId}")
    public R<IotDeviceGroupVo> getInfo(@PathVariable Long groupId) {
        return R.ok(groupService.queryById(groupId));
    }

    /**
     * 新增设备分组
     */
    @PostMapping
    public R<Boolean> add(@Validated(AddGroup.class) @RequestBody IotDeviceGroupBo bo) {
        return R.ok(groupService.insertByBo(bo));
    }

    /**
     * 修改设备分组
     */
    @PutMapping
    public R<Boolean> edit(@Validated(EditGroup.class) @RequestBody IotDeviceGroupBo bo) {
        return R.ok(groupService.updateByBo(bo));
    }

    /**
     * 删除设备分组
     */
    @DeleteMapping("/{groupIds}")
    public R<Boolean> remove(@PathVariable Long[] groupIds) {
        return R.ok(groupService.deleteWithValidByIds(List.of(groupIds), true));
    }
} 